package ch.unisi.inf.pfii.teamwhite.risk.graphic.gameFrame.drawers;

/**
 * 
 * @author Matteo Bellan: bellanm@lu.unisi.ch
 * @author Andrea Michelotti: micheloa@lu.unisi.ch
 * @author Ranganathan Ramasubramanian: ramasubr@lu.unisi.ch
 *
 */

import java.util.ArrayList;

import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;

import ch.unisi.inf.pfii.teamwhite.risk.core.Game;
import ch.unisi.inf.pfii.teamwhite.risk.core.Planet;
import ch.unisi.inf.pfii.teamwhite.risk.core.io.MapReader;
import ch.unisi.inf.pfii.teamwhite.risk.core.player.Human;

public final class PlanetDrawer {

	private Game game;
	
	private ArrayList<JLabel> labels;
	
	private MapReader mapReader;
	private String map;

	public PlanetDrawer(final Game game, final String map) {
		this.game = game;
		
		labels = new ArrayList<JLabel>();
		
		mapReader = new MapReader();
		this.map = map;
	}
		
//	This method creates a label which contains the number of ships the building and the image of the planet
	public final void drawShips(final JPanel imagePanel) {
		int radius = 10;
		for (Planet planet : game.getGalaxy().getPlanets()) {
			String[] coordinates = mapReader.lister(planet.getName(), map);
			int x = Integer.parseInt(coordinates[1]) - radius; 
			int y = Integer.parseInt(coordinates[2]) - radius;
			
			if(planet.getOwner().getType().equals("Gaia")){
				planet.getOwner().setColor("BLACK");
			}
			String path = "planeticons/" + planet.getOwner().getColor() + ".png";
			if(planet.getOwner() != null && planet.getOwner().getType().equals("Human") && ((Human)planet.getOwner()).getSelectedPlanet() != null && ((Human)planet.getOwner()).getSelectedPlanet().equals(planet)){
				path = "planeticons/" + planet.getOwner().getColor() + "selected.png";
			}
			ImageIcon picture = new ImageIcon(getClass().getResource(path));
			JLabel planetPicture = new JLabel(picture);
			
			JLabel shipLabel = new JLabel("" + planet.getShips().size());
			
			JLabel buildingLabel = new JLabel(building(planet));
			
			ImageIcon capital = new ImageIcon(getClass().getResource("planeticons/BLACK.png"));
			if(planet.isCapital()){
				capital = new ImageIcon(getClass().getResource("planeticons/capital.png"));
			}
			
			JLabel capitalLabel = new JLabel(capital);
			capitalLabel.setBounds(x -40, y - 60, radius*10, radius*10);
			imagePanel.add(capitalLabel);
			
			planetPicture.setBounds(x-4*radius - 5, y-4*radius - 5, radius*10, radius*10);
			imagePanel.add(planetPicture);
			
			shipLabel.setBounds(x, y, radius*2, radius*2);
			imagePanel.add(shipLabel);
			labels.add(shipLabel);
			
			buildingLabel.setBounds(x-15, y-15, radius*2, radius*2);
			imagePanel.add(buildingLabel);
			labels.add(buildingLabel);
		}
	}
	
//	This method return the letter to show on the frame depending on the building
	private final String building(final Planet planet) {
		String result = "";
		if (planet.getBuilding().getName().equals("Defensive Shield")) {
			result = "D";
		} else if (planet.getBuilding().getName().equals("Slavery Camp")) {
			result = "S";
		} else if (planet.getBuilding().getName().equals("Tax Center")) {
			result = "T";
		} else if (planet.getBuilding().getName().equals("Eldest Council")) {
			result = "C";
		}
		return result;
	}
	
	public final void clearShips(final JPanel imagePanel) {
		for (JLabel label : labels) {
			imagePanel.remove(label);
		}
		labels.clear();
	}
	
}
